﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Isaac.Joy.Identity.Module;
using Isaac.Joy.Identity.Web.DtCms.Models;
using Isaac.Joy.StringHelper;
using Newtonsoft.Json;

namespace Isaac.Joy.Identity.Web.DtCms.Controllers
{
    [Permission("RoleManage")]
    public class RoleController : RepositoryController
    {
        public ActionResult Index(int? pi, int? ps)
        {
            var pageindex = pi ?? 1;
            if (pageindex <= 0) pageindex = 1;
            var pagesize = ps ?? 10;
            if (pagesize <= 0) pagesize = 1;

            int datacount;
            var list = Repository.QueryRole(pageindex, pagesize, out datacount);

            return View(new RoleListViewModel
            {
                PageInfor = new PageInfor(pageindex, pagesize, datacount),
                Data = list
            });
        }

        public ActionResult Add()
        {
            var permissions = Repository.GetPermissions();

            var dictionary = new Dictionary<string, IEnumerable<Permission>>();
            var group = permissions.GroupBy(x => x.PluginName).ToList();
            group.ForEach(x => dictionary.Add(x.Key, x.ToList()));

            return View(new RoleViewModel
            {
                Role = new Role(),
                Permissions = dictionary
            });
        }

        public ActionResult AjaxAdd(RoleViewModel model)
        {
            model.Role.FlagsValue = FlagsValue.CreateByIds(model.RoleValue);
            var role = Repository.AddRole(model.Role);

            return Content(JsonConvert.SerializeObject(new
            {
                return_code = 0,
                success = role != null
            }));
        }

        public ActionResult AjaxDelete(string strId)
        {
            var ids = strId.SplitToInt(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            var rowcount = Repository.DeleteRole(ids.ToArray());

            return Content(JsonConvert.SerializeObject(new
            {
                return_code = 0,
                success = rowcount > 0
            }));
        }

        public ActionResult Edit(int id)
        {
            var role = Repository.GetRole(id);
            if (role == null) return HttpNotFound();

            var permissions = Repository.GetPermissions();

            var dictionary = new Dictionary<string, IEnumerable<Permission>>();
            var group = permissions.GroupBy(x => x.PluginName).ToList();
            group.ForEach(x => dictionary.Add(x.Key, x.ToList()));

            return View(new RoleViewModel
            {
                Role = role,
                Permissions = dictionary
            });
        }

        [HttpPost]
        public ActionResult AjaxEdit(RoleViewModel model)
        {
            model.Role.FlagsValue = FlagsValue.CreateByIds(model.RoleValue);
            var success = Repository.EditRole(model.Role);

            return Content(JsonConvert.SerializeObject(new
            {
                return_code = 0,
                success
            }));
        }
    }
}
